/*******************************************************************************
 * Copyright 2010 DemandSpot
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.demandspot.utils.searchtree;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.SortedSet;
import java.util.TreeSet;

public class SearchTreeNode<T extends SearchTreeElement> implements Comparable<SearchTreeNode<T>>, Serializable {
    protected SortedSet<T> elements;
    protected int level;
    protected SearchTree<T> parent;
    protected SearchTree<T> tree;
    protected String key;
    protected Comparator<T> sameElementComparator;

    protected SearchTreeNode() {
        elements = new TreeSet<T>();
    }
    
    protected SearchTreeNode(Comparator<T> sameElementComparator) {
        this();
        this.sameElementComparator = sameElementComparator;
    }
    
	@Override
	public int compareTo(SearchTreeNode<T> o) {
        return this.key.compareTo(o.key);
    }

    protected void setElements(SortedSet<T> elements) {
        this.elements = elements;
    }

    protected SortedSet<T> getElements() {
        return elements;
    }

    protected void setLevel(int level) {
        this.level = level;
    }

    protected int getLevel() {
        return level;
    }

    protected void setParent(SearchTree<T> parent) {
        this.parent = parent;
    }

    protected SearchTree<T> getParent() {
        return parent;
    }

    protected void setKey(String key) {
        this.key = key;
    }

    protected String getKey() {
        return key;
    }

    protected void addElement(T element) {
        getElements().add(element);
    }

    protected void setTree(SearchTree<T> tree) {
        this.tree = tree;
    }

    protected SearchTree<T> getTree() {
        return tree;
    }
    
    protected List<T> getResultList(int maxResluts) {
        TreeSet<T> answer = sameElementComparator == null? new TreeSet<T>() : new TreeSet<T>(sameElementComparator);
        
        for (T obj : elements) {
            if(maxResluts == -1 || answer.size() < maxResluts) {
                answer.add(obj);
            } else {
                break;
            }
        }
        
        ArrayList<T> toReturn = new ArrayList<T>(answer);
        
        if (sameElementComparator != null) {
        	Collections.sort(toReturn);
        }
        
        return toReturn;
    }
    
    protected List<T> getResultList(String aKey, int maxResults) {
        TreeSet<T> answer = sameElementComparator == null? new TreeSet<T>() : new TreeSet<T>(sameElementComparator);
        String lowercaseKey = aKey.toLowerCase();
        
        for (T obj : elements) {
            String[] objStrings = obj.getKeys();
            
            for(String objString : objStrings) {
	            if(objString.toLowerCase().startsWith(lowercaseKey) &&( maxResults == -1 || answer.size() < maxResults)) {
	                answer.add(obj);
	            }
            }
        }
        
        ArrayList<T> toReturn = new ArrayList<T>(answer);
        
        if (sameElementComparator != null) {
        	Collections.sort(toReturn, sameElementComparator);
        }
        
        return toReturn;
    }
}
